package jdwl.hrmgrsys.action.sys;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import jdwl.hrmgrsys.action.NoticeMsg;
import jdwl.hrmgrsys.action.PageAction;
import jdwl.hrmgrsys.pojo.core.ParaType;
import jdwl.hrmgrsys.pojo.core.SystemPara;
import jdwl.hrmgrsys.pojo.core.SystemParaType;
import jdwl.hrmgrsys.service.sys.SysParamService;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

@SuppressWarnings("rawtypes")
@Controller("sysParamAction")
@Scope("prototype")
/**
 * 系统参数管理
 * @author Huangdian
 *
 */
public class SystemParamAction extends PageAction {
	private static final long serialVersionUID = 1L;
	@Resource
	private SysParamService service;
	private SystemParaType type;
	private SystemPara param;
	public SystemPara getParam() {
		return param;
	}

	public void setParam(SystemPara param) {
		this.param = param;
	}

	public SysParamService getService() {
		return service;
	}
	
	public void setService(SysParamService service) {
		this.service = service;
	}
	
	@SuppressWarnings("unchecked")
	public String listParamType(){
		LinkedHashMap<String, String> orderBy = getOrderBy();
		orderBy.put("id", "desc");
		setResult(getService().listParaTypes(getPageNo(),getiDisplayLength(), null,null,
				orderBy));	
		return SUCCESS;
	}
	
	public String addParamType(){
		getService().addParamType(type);
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String listParam(){
		
		LinkedHashMap<String, String> orderBy = getOrderBy();
		LinkedHashMap<String, String> paramMap=new LinkedHashMap<String, String>();
		List<String> whereClause=new ArrayList<String>();
		String paraTypeCode=getParameter("paraType");
		if(paraTypeCode.length()>0){
			whereClause.add("type.code = :paraType");
			paramMap.put("paraType",paraTypeCode);
		}
		orderBy.put("id", "desc");
		setResult(getService().listParas(getPageNo(),getiDisplayLength(), whereClause,paramMap,
				orderBy));	
		return SUCCESS;
	}
	
	
	public String queryParamForEdit(){
		String pid=getParameter("pid");
		param=(SystemPara)getService().get(SystemPara.class, new Long(pid));
		if(param==null){
			notice(NoticeMsg.WARN,"没有查询到对应参数: pid="+pid);
			return ERROR;
		}
		return "edit";	
	}
	public String queryParamTypeForEdit(){
		String tid=getParameter("tid");
		type=(SystemParaType)getService().get(SystemParaType.class, new Integer(tid));
		if(type==null){
			notice(NoticeMsg.WARN,"没有查询到对应参数: tid="+tid);
			return ERROR;
		}
		return "edit";	
	}

	public String modifyParam(){
		int tid=type.getId();
		SystemParaType paraType=(SystemParaType)service.getDao().load(SystemParaType.class, tid);
		SystemPara param=getParam();
		param.setType(paraType);
		getService().addParam(param);
		notice(NoticeMsg.SUCCESS, "操作成功");
		return SUCCESS;
	}
	
	public String addParam(){
		int tid=type.getId();
		SystemParaType paraType=(SystemParaType)service.getDao().load(SystemParaType.class, tid);
		SystemPara param=getParam();
		param.setType(paraType);
		getService().addParam(param);
		return SUCCESS;
	}
	public String deleteParam(){
		String pid=getParameter("pid");
		SystemPara param=(SystemPara)getService().getDao().load(SystemPara.class, new Long(pid));
		getService().getDao().delete(param);
		return SUCCESS;
	}

	public SystemParaType getType() {
		return type;
	}

	public void setType(SystemParaType type) {
		this.type = type;
	}
}
